import { LOCAL_KEY } from './index.js'
import { message, http } from './utlis.js'
export default {
	name: 'exportFlie',
	template: `#exportFlie`,
	setup() {
		const download = list => {
			const strlist = ['[']
			list.forEach(item => {
				const itemstr = JSON.stringify(item)
				strlist.push(itemstr)
				strlist.push(',')
			})
			strlist.push(']')
			const blob = new Blob(strlist, { type: 'application/json' })
			const url = URL.createObjectURL(blob)
			const el = document.createElement('a')
			el.download = '小说数据.json'
			el.href = url
			el.id = 'download'
			document.body.appendChild(el)
			el.click()
			setTimeout(() => {
				document.querySelector('#download').remove()
				URL.revokeObjectURL(url)
				message('下载成功')
			}, 5000)
		}

		async function exportallclk() {
			const result = await http('/novel')
			console.log(result)
			download(result)
		}
		function exportlocalclk() {
			const datalist = JSON.parse(localStorage.getItem(LOCAL_KEY.NOVEL_DATA))
			const localdata = JSON.parse(localStorage.getItem(LOCAL_KEY.NOVEL_LOCAL_DATA)) ?? []
			download([...datalist, ...localdata])
		}
		return { exportallclk, exportlocalclk }
	},
}
